package leetcode.code5998;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution6000 implements IDebug {

	public long goodTriplets(int[] nums1, int[] nums2) {
		int len = nums1.length;
		long[][] hash = new long[len][3];
		long twice = 0;
		long none = len;
		long ans = 0;
		for (int i = 0; i < len; i++) {
//			[9,2,0,8,7,1,3,6,5,4]
//			[0,1,8,2,3,9,4,5,6,7]
			int num1 = nums1[i];
			int num2 = nums2[i];
			if (num1 != num2) {
				int sub = 0;
				if (hash[num1][0] == 0) {
					sub += 1;
					hash[num1][0] = 1;
					if (hash[num2][0] == 1) {
						hash[num1][1] = twice + 1;
					} else {
						hash[num1][1] = twice;
					}
				} else if (hash[num1][0] == 1) {
					twice += 1;
					hash[num1][0] = 2;
					hash[num1][2] = none;
					ans += hash[num1][1] * none;
				}
				if (hash[num2][0] == 0) {
					sub += 1;
					hash[num2][0] = 1;
					hash[num2][1] = twice;
				} else if (hash[num2][0] == 1) {
					twice += 1;
					hash[num2][0] = 2;
					hash[num2][2] = none;
					ans += hash[num2][1] * none;
				}
				none -= sub;
			} else {
				none -= 1;
				hash[num1][0] = 2;
				hash[num1][1] = twice;
				hash[num1][2] = none;
				ans += hash[num1][1] * hash[num1][2];
				twice++;
			}
		}
		HelpDebug.print(hash);
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(1, this.goodTriplets(HelpDebug.str2array3("[2,0,1,3]"), HelpDebug.str2array3("[0,1,2,3]")));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(4, this.goodTriplets(HelpDebug.str2array3("[4,0,1,3,2]"), HelpDebug.str2array3("[4,1,0,2,3]")));

	}

	@Override
	public void debug3() {
//		[9,2,0,8,7,1,3,6,5,4]
//		[0,1,8,2,3,9,4,5,6,7]
		HelpDebug.compare(21, this.goodTriplets(HelpDebug.str2array3("[9,2,0,8,7,1,3,6,5,4]"), HelpDebug.str2array3("[0,1,8,2,3,9,4,5,6,7]")));

	}

	@Override
	public void debug4() {
//		[9,2,0,8,7,1,3,6,5,4]
//		[0,1,8,2,3,9,4,5,6,7]
		HelpDebug.compare(1, this.goodTriplets(HelpDebug.str2array3("[0,1,2]"), HelpDebug.str2array3("[0,1,2]")));

	}

	public void debug5() {
//		[3,1,2,0]
//		[3,0,1,2]
		HelpDebug.compare(1, this.goodTriplets(HelpDebug.str2array3("[3,1,2,0]"), HelpDebug.str2array3("[3,0,1,2]")));

	}

	public static void main(String[] args) {
		Solution6000 so = new Solution6000();
//		so.debug5();
//		so.debug4();
		so.debug3();
//		so.debug1();
//		so.debug2();

	}

}
